<template>
  <el-dialog
    v-model="visible"
    :title="title"
    :width="width"
    center
    align-center
    destroy-on-close
    :close-on-click-modal="false"
  >
    <div class="purchase-dialog-content">
      <div class="purchase-header">
        <h2>{{ headerTitle }}</h2>
        <p class="purchase-subtitle">{{ subtitle }}</p>
      </div>
      
      <div class="product-cards-wrapper">
        <el-card 
          v-for="product in products" 
          :key="product.productID" 
          class="product-card"
          :class="{ 'premium-card': isPremiumProduct(product) }"
        >
          <div class="product-header">
            <h3>{{ product.name }}</h3>
            <div class="product-badge" v-if="isPremiumProduct(product)">推荐</div>
          </div>
          
          <div class="product-description">
            <p>{{ product.description }}</p>
          </div>
          
          <div class="product-features" v-if="showFeatures">
            <ul>
              <li v-for="feature in getFeatures(product)" :key="feature">✓ {{ feature }}</li>
            </ul>
          </div>
          
          <div class="price">¥{{ product.price }}</div>
          
          <el-button 
            type="primary" 
            size="large"
            class="purchase-button"
            :loading="purchasing"
            @click="handlePurchase(product.productID)"
          >
            {{ purchasing ? '购买中...' : '立即购买' }}
          </el-button>
        </el-card>
      </div>
    </div>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  products: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '购买商品'
  },
  headerTitle: {
    type: String,
    default: '选择您的套餐'
  },
  subtitle: {
    type: String,
    default: '选择最适合您的套餐'
  },
  width: {
    type: String,
    default: '60%'
  },
  showFeatures: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'purchase'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const purchasing = ref(false)

const isPremiumProduct = (product) => {
  return product.price > 100
}

const getFeatures = (product) => {
  // 根据产品类型返回不同的特性列表
  if (product.name && product.name.includes('VIP')) {
    return [
      '无限制课程学习',
      '优先客服支持', 
      '专属学习资料',
      '学习进度同步'
    ]
  }
  return [
    '基础功能使用',
    '标准客服支持',
    '基础学习资料'
  ]
}

const handlePurchase = async (productID) => {
  purchasing.value = true
  try {
    emit('purchase', productID)
  } catch (error) {
    console.error('Purchase error:', error)
  } finally {
    purchasing.value = false
  }
}

const handleClose = () => {
  visible.value = false
}
</script>

<style scoped>
.purchase-dialog-content {
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  border: 1px solid #eee;
}

.purchase-header {
  text-align: center;
  margin-bottom: 30px;
}

.purchase-header h2 {
  margin-bottom: 10px;
  color: #333;
  font-size: 24px;
}

.purchase-subtitle {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.product-cards-wrapper {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: center;
}

.product-card {
  width: 280px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  border-radius: 8px;
  text-align: center;
  padding: 20px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  overflow: hidden;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.premium-card {
  border: 2px solid #e6a23c;
  background: linear-gradient(135deg, #fff 0%, #fff8e1 100%);
}

.premium-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #e6a23c, #f39c12);
}

.product-header {
  position: relative;
  margin-bottom: 15px;
}

.product-header h3 {
  color: #333;
  margin: 0;
  font-size: 1.4em;
}

.product-badge {
  position: absolute;
  top: -10px;
  right: -10px;
  background: linear-gradient(45deg, #e6a23c, #f39c12);
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
}

.product-description {
  margin-bottom: 15px;
}

.product-description p {
  color: #666;
  font-size: 0.9em;
  margin: 0;
  min-height: 40px;
  line-height: 1.4;
}

.product-features {
  margin-bottom: 20px;
  text-align: left;
}

.product-features ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.product-features li {
  color: #666;
  font-size: 13px;
  margin-bottom: 8px;
  padding-left: 0;
}

.product-features li::before {
  content: '✓';
  color: #67c23a;
  font-weight: bold;
  margin-right: 8px;
}

.price {
  font-size: 1.8em;
  color: #e6a23c;
  font-weight: bold;
  margin-bottom: 20px;
}

.purchase-button {
  width: 100%;
  background: linear-gradient(45deg, #409EFF, #66b1ff);
  border: none;
  font-weight: bold;
  transition: all 0.3s ease;
}

.purchase-button:hover {
  background: linear-gradient(45deg, #66b1ff, #409EFF);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.purchase-button:disabled {
  background: #c0c4cc;
  transform: none;
  box-shadow: none;
}
</style> 